﻿using DeZhen.DataCollection.IService.DataSourceSetting;
using DeZhen.DataCollection.Model.Common;
using DeZhen.DataCollection.Model.DTO.DataSourceSettings;
using DeZhen.DataCollection.Model.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;

namespace DeZhen.DataCollection.WebApi.Controllers
{
    /// <summary>
    /// 数据源设置
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DataSourceSettingsController : ControllerBase
    {
        private readonly IDataSourceSettingsSerevice _settingsSerevice;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="settingsSerevice"></param>
        public DataSourceSettingsController(IDataSourceSettingsSerevice settingsSerevice)
        {
            _settingsSerevice = settingsSerevice;
        }

        /// <summary>
        /// 获取数据源设置分页列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpGet(Name = "GetDataSourceSettingsPaged")]
        public async Task<PageResult<DataSourceSettingsDTO>> GetDataSourceSettingsPagedAsync([FromQuery] QueryDataSourceSettings param)
        {
            return await _settingsSerevice.GetListToPageAsync(param);
        }

        /// <summary>
        /// 根据ID获取数据源设置对象DTO
        /// </summary>
        /// <param name="id">数据源设置ID</param>
        /// <returns></returns>
        [HttpGet(Name = "GetSettingsById")]
        public async Task<DataSourceSettingsDTO> GetSettingsByIdAsync([Required] long id)
        {
            return await _settingsSerevice.GetSettingsByIdAsync(id);
        }

        /// <summary>
        /// 添加数据源设置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost(Name = "Add")]
        public async Task<bool> AddAsync(DataSourceSettingsAdd input)
        {
            return await _settingsSerevice.AddAsync(input);
        }

        /// <summary>
        /// 更新数据源设置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost(Name = "Update")]
        public async Task<bool> EditAsync(DataSourceSettingsEdit input)
        {
            return await _settingsSerevice.UpdateAsync(input);
        }

        /// <summary>
        /// 删除数据源设置
        /// </summary>
        /// <param name="id">数据源ID</param>
        /// <returns></returns>
        [HttpPost(Name = "Delete")]
        public async Task DeleteAsync(long id)
        {
            await _settingsSerevice.DeleteAsync(id);
        }

        /// <summary>
        /// 测试数据源连接
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet(Name = "DatasourceConnect")]
        public async Task<bool> DatasourceConnectAsync([FromQuery] DataSourceSettingsAdd input)
        {
            return await _settingsSerevice.TestConnectionAsync(input);
        }

        /// <summary>
        /// 获取数据源设置下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet(Name = "GetSettingsDropDown")]
        public async Task<List<DataSourceSettingsDropDownDTO>> GetSettingsDropDownAsync()
        {
            return await _settingsSerevice.GetDropDownListAsync();
        }
    }
}
